package ui.gui;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 * @author Piesold
 */
public class GuiBuilder {

	private static GuiBuilder INSTANCE;

	private Dimension sizeBtn;
	private Dimension sizeComboBox;
	private Dimension sizeTextField;
	private Dimension sizeLabel;

	private Container parentContainer;

	/**
	 * create new GuiBuilder
	 */
	private GuiBuilder() {
		sizeBtn       = new Dimension( 120, 25 );
		sizeComboBox  = new Dimension( 150, 25 );
		sizeTextField = new Dimension( 150, 25 );
		sizeLabel     = new Dimension( 120, 25 );

		parentContainer = null;
	}

	/**
	 * @return INSTANCE to this class
	 */
	public static GuiBuilder getInstance( ) {
		if ( INSTANCE == null )
			INSTANCE = new GuiBuilder();
		return INSTANCE;
	}
	@Override
	protected Object clone() throws CloneNotSupportedException {
		return new CloneNotSupportedException();
	}



	/** @return the sizeBtn */
	public Dimension getSizeBtn() {
		return this.sizeBtn;
	}
	/** @param sizeBtn the sizeBtn to set */
	public void setSizeBtn(Dimension sizeBtn) {
		this.sizeBtn = sizeBtn;
	}

	/** @return the sizeComboBox */
	public Dimension getSizeComboBox() {
		return this.sizeComboBox;
	}
	/** @param sizeComboBox the sizeComboBox to set */
	public void setSizeComboBox(Dimension sizeComboBox) {
		this.sizeComboBox = sizeComboBox;
	}

	/** @return the sizeTextField */
	public Dimension getSizeTextField() {
		return this.sizeTextField;
	}
	/** @param sizeTextField the sizeTextField to set */
	public void setSizeTextField(Dimension sizeTextField) {
		this.sizeTextField = sizeTextField;
	}

	/** @return the sizeLabel */
	public Dimension getSizeLabel() {
		return this.sizeLabel;
	}
	/** @param sizeLabel the sizeLabel to set */
	public void setSizeLabel(Dimension sizeLabel) {
		this.sizeLabel = sizeLabel;
	}

	/** @return the parentContainer */
	public Container getParentContainer() {
		return this.parentContainer;
	}
	/** @param parentContainer the parentContainer to set */
	public void setParentContainer(Container parentContainer) {
		this.parentContainer = parentContainer;
	}
	public void unsetParentContainer() {
		this.parentContainer = null;
	}

	/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */


	/**
	 * ceates a <code>JComboBox</code>
	 * @param location the location for the <code>JComboBox</code>
	 * @return the new <code>JComboBox</code>
	 */
	public JComboBox createJComboBox( Point location, Object[] items ) {

		JComboBox b = new JComboBox( items );
		b.setSize( this.sizeComboBox );
		b.setLocation( location );

		if ( this.parentContainer != null )
			this.parentContainer.add(b);

		return b;
	}
	/**
	 * ceates a <code>JTextField</code>
	 * @param location the location for the <code>JTextField</code>
	 * @return the new <code>JTextField</code>
	 */
	public JTextField createJTextField( Point location ) {

		JTextField t = new JTextField();
		t.setSize( this.sizeTextField );
		t.setLocation( location );
		t.setBorder( BorderFactory.createLineBorder( Color.BLACK ) );

		if ( this.parentContainer != null )
			this.parentContainer.add(t);

		return t;
	}
	/**
	 * ceates a <code>JTextField</code>
	 * @param location the location for the <code>JTextField</code>
	 * @param watenmark the watermark for this textfield
	 * @return the new <code>JTextField</code>
	 */
	public JTextField createJTextField( Point location, String watermark ) {

		final String wm = watermark;
		final JTextField t = new JTextField( wm );
		t.setSize( this.sizeTextField );
		t.setLocation( location );
		t.setForeground( Color.LIGHT_GRAY );
		t.setBorder( BorderFactory.createLineBorder( Color.BLACK ) );

		t.addFocusListener( new FocusListener() {
			@Override public void focusLost(FocusEvent arg0) {
				if ( t.getText().trim().length() == 0 ) {
					t.setForeground( Color.LIGHT_GRAY );
					t.setText( wm );
				}
			}
			@Override public void focusGained(FocusEvent arg0) {
				if ( t.getText().trim().equalsIgnoreCase( wm ) ) {
					t.setForeground( Color.BLACK );
					t.setText( "" );
				}
			}
		});

		if ( this.parentContainer != null )
			this.parentContainer.add(t);

		return t;
	}
	/**
	 * ceates a <code>JLabel</code>
	 * @param location the location for the <code>JLabel</code>
	 * @param text the text that the <code>JLabel</code> should show
	 * @return the new <code>JLabel</code>
	 */
	public JLabel createJLabel( Point location, String text ) {

		JLabel l = new JLabel( text );
		l.setSize( this.sizeLabel );
		l.setLocation( location );

		if ( this.parentContainer != null )
			this.parentContainer.add(l);

		return l;
	}
	/**
	 * creates a Button
	 * @param location location for the button
	 * @param text text the button should show
	 * @param al ActionListener for the button
	 * @return the new button
	 */
	public JButton createJButton( Point location, String text, ActionListener al ) {
		JButton btn = new JButton( text );

		btn.setLocation( location );
		btn.setSize( this.sizeBtn );
		btn.addActionListener( al );

		if ( this.parentContainer != null )
			this.parentContainer.add(btn);

		return btn;
	}

}
